import time
import requests
import json
from tools.funciton import read_xlsx_file, read_json_file
import os
from config.configs import DATA_PATH

addresses = read_json_file(os.path.join(DATA_PATH, '87receive.json'))


def get_param(address):
    """地址解析，返回数组:[姓名,电话,地址]"""
    url = "https://dc-oa-dev-0703.dingstock.net/express/address/parse"
    payload = json.dumps({
        "address": address
    })
    headers = {
        'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
        'Content-Type': 'application/json',
        'token': 'eyJhbGciOiJIUzI1NiJ9.eyJpZCI6InRQZ2ptS0dKVHoiLCJyb2xlIjoidXNlciIsImlhdCI6MTcxNjg5ODcxNH0.-3t9CeKMSbY4XXMIurWZOZuQ6iR-es8eNnYby36UAFc',
        'Accept': '*/*',
        'Host': 'dc-oa-dev-0703.dingstock.net',
        'Connection': 'keep-alive'
    }
    response = requests.request("POST", url, headers=headers, data=payload).json()
    province = response['data']['province']
    city = response['data']['city']
    county = response['data']['county']
    street = response['data']['street']
    detail = response['data']['address']
    result = f'{province}{city}{county}{street}{detail}'
    return [response['data']['name'], response['data']['phone'], result]



def yida_batch_create_order(yidaorder):
    url = 'https://apiv2.dingstock.net/express/yida'
    headers = {
       'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
       'Content-Type': 'application/json',
       'token': 'eyJhbGciOiJIUzI1NiJ9.eyJpZCI6InRUZE1mTWh6b04iLCJyb2xlIjoidXNlciIsImlhdCI6MTcxODI3NzYzNX0.1iUG8l6iAZNMuffS6pgW4fe6UbDDyjOX31MM7va5QYo',
       'Accept': '*/*',
       'Host': 'apiv2.dingstock.net',
       'Connection': 'keep-alive'
    }
    for i in range(len(addresses)):
        param = {
          "thirdNo": f"yida{yidaorder}",
          "deliveryType": "YTO",
          "customerType": "kd",
          "deliveryBusiness": "YTO_BK",
          "senderName": "盯链",
          "senderMobile": "18621726744",
          "senderProvince": "四川省",
          "senderCity": "成都市",
          "senderDistrict": "武侯区",
          "senderAddress": "天府软件园C区2栋5楼盯潮",
          "receiveName": addresses[i]['username'],
          "receiveMobile": addresses[i]['mobile'],
          "receiveProvince": addresses[i]['province'],
          "receiveCity": addresses[i]['city'],
          "receiveDistrict": addresses[i]['district'],
          "receiveAddress": addresses[i]['address'],
          "goods": "日用品",
          "weight": 1,
          "packageCount": 1
        }
        yidaorder += 1
        r = requests.post(url=url, headers=headers, data=json.dumps(param)).json()
        if r['data']['msg'] != '操作成功':
            print(r)
        time.sleep(3)

